package _02_linked_list;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author: mornd
 * @dateTime: 2023/5/31 - 14:26
 * 双向环形链表 共一个哨兵即代表头也代表尾
 */
public class DoublyRingLinkedListSentinel<E> implements Iterable<E> {
    // 哨兵节点，既是头哨兵也是尾哨兵
    private final Node<E> sentinel = new Node<>(null, null, null);

    /**
     * 构造器，初始化哨兵
     */
    public DoublyRingLinkedListSentinel() {
        sentinel.prev = sentinel.next = sentinel;
    }

    public E get(int index) {
        Node<E> node = findNode(index);
        return node.value;
    }

    public Node<E> getFirst() {
        return findNode(0);
    }
    public Node<E> getLast() {
        Node<E> prev = sentinel.prev;
        if(prev == sentinel) {
            throw new NoSuchElementException();
        }
        return prev;
    }

    private Node<E> findNode(int index) {
        if(index < 0) {
            throw illegalIndex(index);
        }
        Node<E> s = sentinel.next;
        for (int i = 0; s != sentinel; i++) {
            if(i == index) {
                return s;
            }
            s = s.next;
        }
        throw illegalIndex(index);
    }

    public E set(int index, E e) {
        Node<E> node = findNode(index);
        E value = node.value;
        node.value = e;
        return value;
    }

    public void add(int index, E e) {

    }

    public void add(E e) {
        addLast(e);
    }

    public void addFirst(E e) {
        Node<E> next = sentinel.next;
        Node<E> newNode = new Node<>(sentinel, e, next);
        sentinel.next = newNode;
        next.prev = newNode;
    }

    public void addLast(E e) {
        Node<E> prev = sentinel.prev;
        Node<E> newNode = new Node<>(prev, e, sentinel);
        prev.next = newNode;
        sentinel.prev = newNode;
    }

    public void remove(int index) {
        Node<E> p = findNode(index);
        Node<E> prev = p.prev;
        Node<E> next = p.next;
        prev.next = next;
        next.prev = prev;
    }

    public void removeFirst() {
        remove(0);
    }

    public void removeLast() {
        Node<E> removed = sentinel.prev;
        if(removed == sentinel) {
            throw new NoSuchElementException();
        }
        Node<E> prev = removed.prev;
        prev.next = sentinel;
        sentinel.prev = prev;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node<E> next = sentinel.next;
        while (next != sentinel) {
            sb.append(next.value);
            if(next.next != sentinel) {
                sb.append(",");
            }
            next = next.next;
        }
        return sb.append("]").toString();
    }

    /**
     * 下标不合法
     * @param index
     * @return
     */
    private IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("下标 %d 不合法", index));
    }

    @Override
    public Iterator<E> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator<E> {
        Node<E> p = sentinel.next;
        @Override
        public boolean hasNext() {
            return p != sentinel;
        }

        @Override
        public E next() {
            E value = p.value;
            p = p.next;
            return value;
        }
    }

    /**
     * 节点元素
     */
    private static class Node<E> {
        Node<E> prev;
        E value;
        Node<E> next;

        public Node(Node<E> prev, E value, Node<E> next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }

        @Override
        public String toString() {
            return String.valueOf(value);
        }
    }
}
